package com.sinopec.basemodule.dialog;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;

import com.sinopec.basemodule.R;
import com.sinopec.basemodule.widget.popupwindow.BasePopup;

/**
 * @author lwj
 * @ClassName: CountDownPopupWindow
 * @Description: 倒计时弹窗
 */
public class CountDownPopupWindow extends BasePopup<CountDownPopupWindow> {
    private Context mContext;
    //监听事件回调
    private OnSelectListener listener;
    //图标
    private ImageView iv_icon;
    //标题
    private TextView tv_title;
    //内容
    private TextView tv_content;
    //确认按钮
    private TextView tv_confirm;
    //取消按钮
    private TextView tv_cancel;
    //本地资源图标
    private @DrawableRes int resId;
    //标题
    private String title;
    //内容
    private String content;
    //确认按钮文字
    private String confirmText;
    //取消按钮文字
    private String cancelText;

    public static CountDownPopupWindow create(Context context, @DrawableRes int resId, String title, String content, String confirmText, String cancelText, OnSelectListener listener) {
        return new CountDownPopupWindow(context, resId, title, content, confirmText, cancelText, listener);
    }

    public CountDownPopupWindow(Context context, @DrawableRes int resId, String title, String content, String confirmText, String cancelText, OnSelectListener listener) {
        this.listener = listener;
        this.mContext = context;
        this.resId = resId;
        this.title = title;
        this.content = content;
        this.confirmText = confirmText;
        this.cancelText = cancelText;
        setContext(context);
    }


    @Override
    protected void initAttributes() {
        setContentView(R.layout.count_down_popupwindow, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusAndOutsideEnable(false)
                .setBackgroundDimEnable(true)
                .setNeedReMeasureWH(true)
                .setDimValue(0.5f);
    }

    @Override
    protected void initViews(View view, CountDownPopupWindow basePopup) {
        //图标
        iv_icon = view.findViewById(R.id.iv_icon);
        if(resId != 0){
            iv_icon.setVisibility(View.VISIBLE);
            iv_icon.setImageResource(resId);
        }else{
            iv_icon.setVisibility(View.GONE);
        }

        //标题
        tv_title = view.findViewById(R.id.tv_title);
        if(!TextUtils.isEmpty(title)){
            tv_title.setVisibility(View.VISIBLE);
            tv_title.setText(Html.fromHtml(title));
        }else{
            tv_title.setVisibility(View.GONE);
        }
        //内容
        tv_content = view.findViewById(R.id.tv_content);
        if(!TextUtils.isEmpty(content)){
            tv_content.setVisibility(View.VISIBLE);
            tv_content.setText(Html.fromHtml(content));
        }else{
            tv_content.setVisibility(View.GONE);
        }
        //确认按钮
        tv_confirm = view.findViewById(R.id.tv_confirm);
        if(!TextUtils.isEmpty(confirmText)){
            tv_confirm.setVisibility(View.VISIBLE);
            tv_confirm.setText(Html.fromHtml(confirmText));
        }else{
            tv_confirm.setVisibility(View.GONE);
        }
        //取消按钮
        tv_cancel = view.findViewById(R.id.tv_cancel);
        if(!TextUtils.isEmpty(cancelText)){
            tv_cancel.setVisibility(View.VISIBLE);
            tv_cancel.setText(Html.fromHtml(cancelText));
        }else{
            tv_cancel.setVisibility(View.GONE);
        }

        //确认按钮事件
        tv_confirm.setOnClickListener(v -> {
            listener.onConfirm();
        });

        //取消按钮事件
        tv_cancel.setOnClickListener(v -> {
            listener.onCancel();
            //dismiss();
        });

    }

    public interface OnSelectListener {
        /**
         * 确认
         */
        void onConfirm();

        /**
         * 取消
         */
        void onCancel();
    }
}
